/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;

public class Main
{
    //TEST CASES*********************************************************************8
    //static int []nums = new int[]{100,4,200,1,2,6,7,8,9,100,101,102,103};
    //static int []nums = new int[]{100,4};
    //static int []nums = new int[] {1,2,3,4};
   //static int []nums = new int[] {8,1,6,2,9,7,3,100,4,5};
   //static int []nums = new int[] {10,11,12,50,51,52};
    //static int []nums = new int[]{1,1,1,1,1,1};
    //static int []nums = new int[]{100,4,200,1,0,3,2};
    
    //static int []nums = new int[] {5};
    //static int []nums = new int[] {100,4};
    //static int []nums = new int[] {1,2,3,4};
    //static int []nums = new int[] {100,4,200,1,3,2};
    //static int []nums = new int[] {1,2,2,3};
    //static int []nums = new int[] {10,11,12,50,51,52};
    //static int []nums = new int[] {8,1,6,2,9,7,3,100,4,5};
    //static int []nums = new int[] {0,1,2,3};
    //static int []nums = new int[] {100,4,200,1,0,3,2};
    //static int []nums = new int[] {-3,-2,-1,5,6};
    //static int []nums = new int[] {1,1,1,1
    //static int []nums = new int[] {};
    //static int []nums = new int[] {1,10,2,20,3,30};
    //static int []nums = new int[] {5,6,7,50,51,52,53,1,2,3,4};
    //static int []nums = new int[] {9,1,4,7,3,2,6,5,8,8,2,1};
    //static int []nums = new int[] {0};
    //static int []nums = new int[] {0,0,0};
    //static int []nums = new int[] {-1,0,1,2};
    //static int []nums = new int[] {-2,-1,0,2,1,10};
    //static int []nums = new int[] {0,2,4,6};
    //static int []nums = new int[] {0,1,1,2,2,3,3,4};
    //static int []nums = new int[] {0,5,6,7,8};
    //static int []nums = new int[] {-5,-4,-3,-2,-1,0};
    //static int []nums = new int[] {0,1,2,50,51,52,53,54,55};
    
    //TORTURE TESTS
    static int []nums = new int[] {2,3,4,0,1};
    
    
    
    //Since we can not be sure how long each sequence will be, we have to use the worst case scenario to determine
    //that each number might have no consecutive chains
    
    //and also since all elements in nums can be consecutive, we have to assume that
    //a single consecutive chain can be populated  stored in any index between (0-nums.length-`1)`  Store[0][nums.length]
    //it has no knowledge about duplicate values such as
    //static int []nums = new int[] {1,2,2,3},  so its one size fits all
    static int[][] Store = new int[nums.length][nums.length];
    
    //need to phase out since String
    //static String [] temp = new String[nums.length];
    //this now needs to be a 3D array to hold  static int[][] Store
    //need to be careful in that there is  a single 2D array  at each location in the 3D array
    static int [][][] storeMaxConsecutiveSequences = new int [nums.length][1][nums.length];
    
    static int differenceCheck;
    static int count=0;
    static int firstNum;
    static int nextNum;
    
    static int countSequencesStored;
    
    public static boolean nextnumbercheck(int firstNum)
    {
        for (int i=0; i<nums.length; i++)
        {
            if (nextNum==nums[i])
            {
                count++;
                Store[firstNum][count]=nextNum;
                return true;
            }
        }
        count=0;
        return false;
    } //end of method
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        boolean nextnumberconsecutive=false;
        
        for (int i=0; i< nums.length; i++)
        {
            Store[i][count]=nums[i];
            System.out.println("\nThis is the number being checked: " + nums[i]);
            differenceCheck=1;
            
            for (int j=0; j<nums.length; j++)
            {
                if (j==i) 
                {
                    j++;
                }
                if (j!=nums.length)
                {
                    if (nums[j]==nums[i]+differenceCheck)
                    {
                        count++;  
                        do
                        {
                            System.out.println("Next consecutive number has appeared: " + (nums[i]+differenceCheck));
                            Store[i][count]= nums[i]+differenceCheck;
                            differenceCheck++;
                            nextNum = nums[i]+differenceCheck;  //next consecutive number expected....
                            nextnumberconsecutive = nextnumbercheck(i);
                        }while(nextnumberconsecutive);
                        
                    }
                }
            }  //end of inner for loop
        }  //end of for loop going through all nums
   
        System.out.println("\n\nLength of store: " + Store.length);
        System.out.println("********These are all entries*********");
        
        for (int max[]: Store)   
        {
            System.out.println(Arrays.toString(max));
        }
        
        StringJoiner sj = new StringJoiner(",");
        int currentMaximumConsectiveNumbers=0;
        String numtoString;
        
        //completed inline with test case 3
        //boolean conditionOnce=false;
        int currentI=0;
        int j=0;
        int zeroFoundLocation=0;
        boolean zeroFound=false;
        int posZero=0;
        
        //now introduced as part of transitioning into integer
        int index;
        
        //Completed inline with test case 3
        //int oldConsecutiveNumbers=0;
        String backupStringJoiner;
        int countIndexLocationsNoZero=0;
        
        //introduced inline with moving away from Strings
        int [] currentConsecutiveElements = new int[nums.length];
        //int [] backupConsecutiveElements = new int[nums.length];
        
        int captureIndex=0;
        
        int backupPosZero=0;
        
        boolean hasExecuteOnce=false;
        boolean hasZeroFound=false;
        
        
        for (int i=0; i<Store.length; i++)
        {
            System.out.println("\n******NEW NUMBER*****:  " + Arrays.toString(Store[i]));
            System.out.println("This is current highest consecutive Numbers in sequence stored: " + currentMaximumConsectiveNumbers);
            System.out.println("TRACK poszero: " + posZero);
            
            hasExecuteOnce=false;
            
            backupPosZero=posZero;
            
            captureIndex=0;
            //no longer required, moving away Strings
            //sj=new StringJoiner(",");
            countIndexLocationsNoZero=0;
            index=0;
            
            //we now have to also reset this 
            //posZero=0;
            
            for (j=0; j<Store[0].length; j++)
            {
                
                //need to consider phasing out Strings
                //it refers to single index location
                //numtoString=Integer.toString(Store[i][j]);
                
                int elementInArray = Store[i][j];
                
                
                zeroFound=false;
                
                //no longer required
                //conditionOnce=true;
                
                //this requires more effort to phase out
                //backupStringJoiner = sj.toString();
                //sj.add(numtoString);
                
                //backupConsecutiveElements = currentConsecutiveElements;
                currentConsecutiveElements[index] = elementInArray;
                
                index++;
                
                
                
                //System.out.println(Store[i][j]);
                //System.out.println(j);
                
                //System.out.println("----------------------------VALUE IN J: " + j);
                
                
                if (elementInArray==0)
                {
                    System.out.println("Zero found at index: " + j  + " => " + Store[i][j]);
                    
                    
                    
                    //aiming to phase out Strings
                    //sj=new StringJoiner(",");
                    //sj.add(backupStringJoiner);
                    //currentConsecutiveElements=backupConsecutiveElements;
                    
                      captureIndex = j;
                      hasZeroFound=true;
                        
                        if (captureIndex>=posZero)
                        {
                        posZero=captureIndex;
                        }
                        
                        
                
                    //no longer need this structure
                    //to support Test case 4
                    //if (j>zeroFoundLocation)
                    //{
                        zeroFoundLocation=j;
                    //}
                    zeroFound=true;
                    
                    
                    if (j+1<Store[i].length-1)
                    {
                    if (j!=0 && nums.length!=1 && Store[i][j+1]==0)
                    {
                        System.out.println("MAX OUT!!!!!!!!!!!!!!!!!!!");
                    j=nums.length;
                    //break;
                    }
                    }
                    
                    //I performed this change inline with Test case 3
                    //Logic. Most parts have now been scattered across different areas
                    /*
                    if (conditionOnce)
                    {
                        System.out.println("ENTER HERE");
                        oldConsecutiveNumbers = currentMaximumConsectiveNumbers;
                        currentMaximumConsectiveNumbers=zeroFoundLocation;
                        conditionOnce=false;
                    }
                    */
                    
                    System.out.println(j-1);
                    if (j-1>=0)
                    {
                    
                    if (Store[i][j-1]==0 && hasZeroFound)
                    {
                        System.out.println("REACH HERE!!!!!!!!!!!!!!");
                        if (!hasExecuteOnce)
                        {
                            System.out.println("ADJUST VALUE-------------------------------------------");
                        captureIndex=j-1;
                        posZero=posZero-1;
                        zeroFound=false;
                        hasExecuteOnce=true;
                        }
                    }
                    }
                    
                    
                    
                    
                    
                }
                else
                {
                    //System.out.println("111: " + posZero);
                    System.out.println("222No zero found (index: " + j+") " + " => " + Store[i][j]);
                    
                    //if (j>=posZero && countIndexLocationsNoZero>0)
                    //{
                        //System.out.println("WHEN----------------");
                    if (j>=posZero)
                    {
                        
                        
                    posZero=j;
                    System.out.println("CONFIGURE: " + posZero);
                    }
                    //}
                    //else
                    //{
                        //posZero=currentMaximumConsectiveNumbers;
                    //}
                    //zeroFoundLocation=;
                    countIndexLocationsNoZero++;
                    zeroFound=false;
                }
                
                //Changes inline with test case 3
                //I also no longer dealt with oldConsecutiveNumbers (phased out), so it was currentMaximumConsectiveNumbers which
                //now fitted better across the board
                //I have to consider this valid if there is a zerofound=true or if it has traversed entire array and
                //acknowledged no zeros found
                
                
                System.out.println("1TRACK poszero1: " + posZero);
                System.out.println("1zero found: " + zeroFound);
                System.out.println("1value j: " +  j);
                System.out.println("1current max: " + currentMaximumConsectiveNumbers);
                System.out.println("1zerofoundlocation: " + zeroFoundLocation);
                System.out.println(nums.length);
                System.out.println("1countIndexLocationsNoZero: " + countIndexLocationsNoZero);
                //|| (j>=posZero && (j==nums.length-1)
                System.out.println("capture index: " + captureIndex);
                System.out.println("count sequence: " + countSequencesStored);
                System.out.println("nums-1: " + (nums.length-1));
                
                
                    if (posZero>backupPosZero && posZero>captureIndex && (j==nums.length-1 ||j==nums.length))
                {
                    System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
                    posZero=captureIndex;
                }
                
                
                //capture index <poszero && countSequencesStored==0
                
                //or  countsequence>1 && captureIndex>=poszero
                
                //if ((captureIndex<posZero && countSequencesStored==0 && j==nums.length-1 || j==nums.length) || (countSequencesStored>0 && captureIndex>=posZero && j==nums.length-1 ||j==nums.length))
                System.out.println(j);
                
                int lastElementIndex=0;
                
                if (j>nums.length-1)
                {
                    lastElementIndex=j-1;
                }
                else if (j==nums.length-1)
                {
                    lastElementIndex=j;
                }
                
                //if (posZero>currentMaximumConsectiveNumbers)
                System.out.println("INDEX: " + lastElementIndex);
                System.out.println("last: " + Store[i][lastElementIndex]);
                System.out.println("ssss: " + Store[i][lastElementIndex]);
                
                //we surely need exception handling at this point for test case 18
                //since it is performing execution up tostatic int []nums = new int[] {-1,0,*1,2}
                //and then it decides to enter this loop
                
                boolean hasMoreConsecutive=false;
                
                try
                {
                    if (Store[i][j+1]!=0)
                    {
                        hasMoreConsecutive=true;
                    }
                    System.out.println("MORE LEFT--------------------------------------------");
                }
                catch (ArrayIndexOutOfBoundsException s)
                {
                    System.out.println("ENTERING CATCH********************************************************");
                    
                //}
                
                //if (!hasMoreConsecutive)
                //{
                //if ((captureIndex>currentMaximumConsectiveNumbers || nums.length==1  || (captureIndex==posZero 
                //&& countSequencesStored==0 && Store[i][lastElementIndex]==0 && Store[i][nums.length-1]==0))  
                //|| (countSequencesStored==0 && posZero==nums.length-2  /*Store[i].length-2*/))
                
                //I have not had to simplify the above loop to this, since when I completed the try and catch statements
                //it no longer decided to accept the above statements at all. I found this particular strange since test case
                //static int []nums = new int[] {1,2,3,4} had no issues in Main - passes Test case 9.java
                
                if (countIndexLocationsNoZero>currentMaximumConsectiveNumbers)
                
                /*||countIndexLocationsNoZero>currentMaximumConsectiveNumbers && j>=nums.length-*/
                /*||Store[i][j+1]==0 && captureIndex!=0*/
                
                //this would be if 0 at front
                //if (captureIndex==nums.length-1  || countIndexLocationsNoZero==nums.length-1 && countIndexLocationsNoZero!=posZero && posZero==nums.length-1  
                //|| (captureIndex==0 && posZero==(nums.length-1) && !zeroFound && (Store[i][j]==0 || Store[i][0]==0)))
                {
        
        if (j<nums.length-1)
        {
        if (Store[i][j+1]==0)
        {
        System.out.println("1Zero found at index: " + (j+1)  + " => " + Store[i][nums.length-1]);
        countIndexLocationsNoZero=j+1;
        j++;
        }
        else
        {
            System.out.println("No zero found (index: " + (j+1)+") " + " => " + Store[i][j+1]);
            countIndexLocationsNoZero++;
            j++;
        }
        }
        
                   
                // ((zeroFoundLocation>=currentMaximumConsectiveNumbers && zeroFound) 
                //|| ((j==nums.length-1) && !zeroFound && countIndexLocationsNoZero>=posZero))
                //{
                    //if ((Store[i][j-1])==0 && (j==nums.length) && (zeroFoundLocation!=nums.length-1) /*|| (Store[i][0])==0*/)
                    //{
                    
                    
                    //This has now been changed to currentMaximumConsectiveNumbers
                    //System.out.println("This is current highest streak of consecutiveNumbers: " + oldConsecutiveNumbers);
                    System.out.println("This is current highest streak of consecutiveNumbers: " + currentMaximumConsectiveNumbers);
                    
                    //oldConsecutiveNumbers=zeroFoundLocation;
                    //if (j==nums.length-1)
                    //{
                    
                    //For cases such as static int []nums = new int[] {2,3,4,0,1};
                    //it would store 2,3,4  as 2,3,4,0,0
                    //it would progress right to the end since we know that a 0 can be part of descending
                    //sequence anywhere in the chain
                    //So currentMaximumConsectiveNumbers would be 4 at completion...
                    //And currentMaximumConsectiveNumbers is actually 3
                    
                    //so we need to keep track of in the code if it finds two consecutive zeros....
                    
                    currentMaximumConsectiveNumbers=posZero;
                    
                    System.out.println("CHECK1: " + currentMaximumConsectiveNumbers);
                    
                    
                    
                    //}
                    //Fix for test case 1
                    
                    if (j==0 && Store[i].length==0)
        {
             currentMaximumConsectiveNumbers=1;
        }
        
        System.out.println("CHECK: " + currentMaximumConsectiveNumbers);
        //if (j==nums.length-1 && Store[i][0]==0)
        //{
         //   currentMaximumConsectiveNumbers++;
            
        //}
        
        if (captureIndex>currentMaximumConsectiveNumbers)
        {
            System.out.println("CHECK7");
            currentMaximumConsectiveNumbers=captureIndex;
        }
        System.out.println("CHECK3: " + currentMaximumConsectiveNumbers);
        
        if (countIndexLocationsNoZero>currentMaximumConsectiveNumbers)
        {
            currentMaximumConsectiveNumbers=countIndexLocationsNoZero;
            System.out.println("CHECK4: " + currentMaximumConsectiveNumbers);
        }
        
        
        //This is now a fix for test case  {0,1,2,3}
        if (Store[i][0]==0 && countIndexLocationsNoZero==Store[i].length-1)
        {
            currentMaximumConsectiveNumbers++;
            System.out.println("CHECK5: " + currentMaximumConsectiveNumbers);
        }
        
        //I did not want to interrogate the difference like this...
        //But it becomes impossible otherwise since the 0 might be part of descending sequence and appearing..
        //We will never know otherwise if it is part of the sequence or the default value in storage...
        //Having 0 in front is more clear cut as above
        //useful for tests such as static int []nums = new int[] {0,5,6,7,8};
        //where one of the sequences is  5,6,7,8  and it will be populated as  5,6,7,8,0
        //I have not kept tracking of the frequency elements populated in Store for each consecutive sequence
        //So best of my knowledge there is no other known technique
        
        boolean hasAdjust=false;
        
        System.out.println(j-1);
        System.out.println(Store[i][j-1]);
        
        //this outer loop is used to protect
        //static int []nums = new int[] {0,1,1,2,2,3,3,4};
        
        if (j!=nums.length)
        {
        if (Store[i][j]==0 && countIndexLocationsNoZero==Store[i].length-1 && (Store[i][j-1]+1)==Store[i][j])
        {
            currentMaximumConsectiveNumbers++;
            System.out.println("CHECK6: " + currentMaximumConsectiveNumbers);
            //hasAdjust=true;
        }
        }
        
        if (j==currentMaximumConsectiveNumbers && Store[i][j]!=0)
        {
            currentMaximumConsectiveNumbers++;
        }
        
        hasAdjust=false;
        
        
        
        if(Store[i].length==1)
        {
            currentMaximumConsectiveNumbers=1;
        }
                    
                    System.out.println("This is newly identified streak of consecutive Numbers: " + currentMaximumConsectiveNumbers);
                    
                    
                    if (countIndexLocationsNoZero==(nums.length-1))
                    {
                         posZero=nums.length-1;   
                    }
                    else
                    {
                    posZero=countIndexLocationsNoZero;
                    }
                    
                    
                    //Need to consider phasing out String for a numerical challenge
                    //curretemp = new String[nums.length];
                    //temp[0]=sj.toString();
                    //currentConsecutiveElements = new int[nums.length];
                    
                    //no longer valid
                    //System.out.println("Highest consecutive number sequence so far: " + temp[0]);
                    
                    //We need to also clear the exissting array for storeMaxConsecutiveSequences
                    storeMaxConsecutiveSequences = new int [nums.length][1][nums.length];
                    
                    //We know that Store is  2d array [][]
                    //so now, we need to store this in a 3D array in repository...
                    //we know that since the first row of storage is overwritten, it is ok to process as such
                    storeMaxConsecutiveSequences[0][0]= Store[i];
                    
                    //We need to keep a counter here of the number of times that it has stored a consecutive sequence
                    countSequencesStored++;
                    
                    /*
                    int elem=0;
                          for (int[] row: Store)
                    {
                        for (int element: row)
                        {
                            System.out.println("element: " + element);
                            storeMaxConsecutiveSequences[0][1][elem] = element;
                        }
                        
                        elem++;
                    }
                    */
                    
                    //elem=0;
                    
                    //storeMaxConsecutiveSequences[0] = new int[][][] {Store[i][]}; 
                    
                    
                    
                    System.out.println("Highest consecutive number sequence so far: " + Arrays.toString(Store[i]));
                    
                    break;
                }
                }
                
                
                System.out.println("TRACK poszero1: " + posZero);
                System.out.println("zero found: " + zeroFound);
                System.out.println("value j: " +  j);
                System.out.println("current max: " + currentMaximumConsectiveNumbers);
                System.out.println("countIndexLocationsNoZero: " + countIndexLocationsNoZero);
                //|| (j>=posZero && (j==nums.length-1)
                
                
                if (zeroFound && posZero==0)
                {
                    j=0;
                }
                
                /*
                if (j>posZero)
                {
                    System.out.println("fff: " + Store[i][posZero+1]);
                    
                if (Store[i][posZero+1]==0)
                {
                    posZero=posZero+1;
                    
                }
                }
                */
                
                if (posZero>backupPosZero && posZero>captureIndex && (j==nums.length-1 ||j==nums.length))
                {
                    System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
                    posZero=captureIndex;
                }
                //System.out.println(zeroFound);
                //We no longer reference zero found. This is perhaps seems like most glaring mistaken in my old code.
                
                if (!hasMoreConsecutive)
                {
                    
                    
                //if ((j>=posZero &&posZero!=0 && zeroFound && countIndexLocationsNoZero>=posZero && Store[i][j-1]!=0)
                  //  || (countIndexLocationsNoZero==currentMaximumConsectiveNumbers && j==nums.length))
                    
                if (countIndexLocationsNoZero==currentMaximumConsectiveNumbers)    
                {
                    System.out.println("DSDSADSADAS");
                    //if (j!=nums.length-1 && Store[i][j+1]==0)
                    //{
                    System.out.println("posZero: " + posZero);
                    
                    //We need to keep a counter here of the number of times that it has stored a consecutive sequence
                    //This is because we have defined the 3D array as
                    //static int [][][] storeMaxConsecutiveSequences = new int [nums.length][1][nums.length];
                    //So, if it has 1 row of populated information (this is not hypothetically possible if it reaches this loop)
                    //It will show content such as
                    //[[1, 2, 3, 4]]
                    //[[0, 0, 0, 0]]
                    //[[0, 0, 0, 0]]
                    //[[0, 0, 0, 0]]
                    
                    
                    //phased out in relation to eliminating Strings
                    //System.out.println("The following: " + sj.toString() + "      has been stored. \nIt is also equal to current maximum sequence of consecutive numbers: " + currentMaximumConsectiveNumbers);
                    
                    System.out.println("The following: " + Arrays.toString(Store[i]) + "      has been stored. \nIt is also equal to current maximum sequence of consecutive numbers: " + currentMaximumConsectiveNumbers);
                    storeMaxConsecutiveSequences[countSequencesStored][0]= Store[i];
                    
                    //We also know that there will be another entry in storeMaxConsecutiveSequences since this area of code
                    //defines that the current sequence also is same length. 
                    countSequencesStored++;
                    
                    
                    //phased out in relation to eliminating Strings
                    //temp[i]=sj.toString();
                    
                    
                    
                    break;
                    //}
                }
            }
            hasMoreConsecutive=false;
                
            }//end of for
        }//end of for
        
        //We know this output onto the store could only occur if only number that existed in
        //Store[i] was 0s... The number of entries storeMaxConsecutiveSequences[0].length would 
        //give exact information on number 0s... But this becomes irrelevant..
        //Since they are all 0s, the longest streak can only be 1...
        
        if(Store[0].length>1)
        {
        if (storeMaxConsecutiveSequences[0][0][0]==0 && storeMaxConsecutiveSequences[0][0][1]==0)
        {
            System.out.println("All values in initial array are 0");
            currentMaximumConsectiveNumbers=1;
        }
        }
        else
        {
            if (currentMaximumConsectiveNumbers==0)
            {
                System.out.println("Single value in initial array is 0");
                currentMaximumConsectiveNumbers=1;
            }
        }
        
        System.out.println("\n\n****************************");
        System.out.println("Longest consecutive sequence: " + currentMaximumConsectiveNumbers);
        System.out.println("****************************");
         
        //This now requires modification inline with using Integers
        /*
        for (String s: temp)
        {
             if (s!=null)
             {
                 System.out.println(s);
             }
        }
        */
        int counter=0;
        System.out.println("sss: " + countSequencesStored);
        
        //I know from past experiences, in order to display the 
        for (int[][] sequences : storeMaxConsecutiveSequences)
        {
            if (counter==countSequencesStored)
            {
                //break;
            }
            System.out.println(Arrays.deepToString(sequences));
            
            counter++;
        }
        
    }//end of main
}//end of class

